<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\ApiController;
use App\Http\Resources\Admin\ArticleCollection;
use App\Http\Resources\Admin\Article as ArticleResource;
use App\Handlers\ImageUploadHandler;
use App\Models\Article;
use Validator;
use Auth;

class ArticleController extends ApiController
{
    /**
     * 文章列表.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $limit = $request->input('size', 15);

        $articleOrm = Article::orderBy('created_at', 'DESC')->orderBy('sort', 'DESC');
        
        if ($request->filled('status')) {
            $articleOrm->where('status', $request->input('status'));
        }

        if ($request->filled('type')) {
            $articleOrm->where('type', $request->input('type'));
        }


        return new ArticleCollection($articleOrm->paginate($limit));
    }

    /**
     * 新建文章
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $messages = [
            'title.required' => '请输入文章标题',
            'content.required' => '请输入文章内容'
        ];

        $validator = Validator::make($request->all(), [
            'title' => 'required|max:255',
            'content' => 'required'
        ], $messages);

        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }

        $article = new Article();
        $article->title = $request->input('title');
        $article->alias = $request->input('alias');
        $article->sort = $request->input('sort');
        $article->type = $request->input('type');
        $article->status = $request->input('status');
        $article->content = $request->input('content');

        if ($article->save()) {
            return $this->message('文章新建成功');
        } else {
            return $this->failed('文章新建失败');
        }
    }

    /**
     * 文章详情.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $article = Article::find($id);

        return $this->success(new ArticleResource($article));
    }

    /**
     * 更新文章.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $messages = [
            'title.required' => '请输入文章标题',
            'content.required' => '请输入文章内容'
        ];

        $validator = Validator::make($request->all(), [
            'title' => 'required|max:255',
            'content' => 'required'
        ], $messages);

        if ($validator->fails()) {
            return $this->failed($validator->errors()->first());
        }

        $article = Article::find($id);
        if (!$article) {
            return $this->failed('更新失败');
        }

        $article->title = $request->input('title');
        $article->alias = $request->input('alias');
        $article->sort = $request->input('sort');
        $article->status = $request->input('status');
        $article->type = $request->input('type');
        $article->content = $request->input('content');

        if ($article->save()) {
            return $this->message('文章更新成功');
        } else {
            return $this->failed('文章更新失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if (Article::destroy($id)) {
            return $this->message('删除成功');
        } else {
            return $this->failed('删除失败');
        }
    }

    /**
     * 文章图片图片
     * 
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function imageUpload(Request $request, ImageUploadHandler $uploader)
    {
        if ($request->hasFile('img')) {
            $result = $uploader->save($request->file('img'), '', Auth::guard('manager')->user()->id, 1920);
            if ($result) {
                return $this->success($result);
            } else {
                return $this->failed('上传失败');
            }
        } else {
            return $this->failed('没有上传文件');
        }
    }
}
